/**
 * 按钮类组件单独校验权限，通过验证返回dom，否则dom不渲染
 */
import React from 'react';
import { Divider } from 'antd';
import { useModel } from 'umi';
import { getAuthority } from '@/utils/authority';

window.connectModel = (key: any, name: any) => {
  return (WrappedComponent: any) => {
    return (props: any) => {
      const model = useModel(name);
      const data = { [key]: model };
      return <WrappedComponent {...props} {...data} />;
    };
  };
};
export interface IProps {
  authority: string; // 权限码
  children: React.ReactNode; // 校验通过后返回的dom
}
// @window.connectModel('initPageData', '@@initialState')
class Authorized extends React.Component<IProps> {
  constructor(props) {
    super(props);
  }

  componentDidMount() {}

  checkAuthorized = () => {
    const {
      authority,
      children,
      // initPageData: { initialState },
    } = this.props;
    // const loginInfo: any = initialState.currentUser;
    const loginInfo = getAuthority();
    if (
      authority &&
      loginInfo &&
      loginInfo.permissions &&
      loginInfo.permissions.indexOf(authority) > -1
    ) {
      return children;
    }
    return <span></span>;
  };

  render() {
    return <React.Fragment>{this.checkAuthorized()}</React.Fragment>;
  }
}
// 设置表格内的权限标签
export function getAuthorizedLabelList(a_list) {
  // const { initialState, setInitialState } = useModel('@@initialState');
  // const loginInfo = initialState.currentUser;
  const loginInfo = getAuthority();
  // 过滤权限
  const list = [];
  const rtn_list = [];
  a_list.forEach((map) => {
    const { authorized, a_html } = map;
    if (authorized == '') {
      // 不校验权限
      list.push(a_html);
    } else if (
      authorized &&
      loginInfo &&
      loginInfo.permissions &&
      loginInfo.permissions.indexOf(authorized) > -1
    ) {
      // 通过权限校验
      list.push(a_html);
    }
  });
  // 如果多个权限标签。用分隔符|隔开。
  list.forEach((a_html, index) => {
    if (rtn_list.length > 0) {
      rtn_list.push(<Divider key={`divider${index}`} type="vertical" />);
    }
    rtn_list.push(a_html);
  });
  return rtn_list;
}

// 设置表格内更多的权限标签
export function getAuthorizedLabelMoreList(a_list) {
  // 过滤权限
  const list = [];
  const rtn_list = [];
  // const { initialState, } = useModel('@@initialState');
  // const loginInfo = initialState.currentUser;
  const loginInfo = getAuthority();

  a_list.forEach((map) => {
    const { authorized, a_html } = map;
    if (authorized == '') {
      // 不校验权限
      list.push(a_html);
    } else if (
      authorized &&
      loginInfo &&
      loginInfo.permissions &&
      loginInfo.permissions.indexOf(authorized) > -1
    ) {
      // 通过权限校验
      list.push(a_html);
    }
  });
  // 如果多个权限标签。用分隔符|隔开。
  list.forEach((a_html, index) => {
    if (rtn_list.length > 0) {
      rtn_list.push(<Divider key={`divider${index}`} type="horizontal" />);
    }
    rtn_list.push(a_html);
  });
  return rtn_list;
}

// 设置表格内更多的权限标签
export function includesKey(columns, list, map) {
  let isIncludes = false;
  // const { initialState, } = useModel('@@initialState');
  // const loginInfo = initialState.currentUser;
  const loginInfo = getAuthority();
  list.forEach((value, index) => {
    if (value && loginInfo && loginInfo.permissions && loginInfo.permissions.indexOf(value) > -1) {
      isIncludes = true;
    }
  });
  if (isIncludes) {
    columns.push(map);
  }
}

// 是否有权限
export function checkKey(authority) {
  let isIncludes = false;
  // const { initialState, } = useModel('@@initialState');
  // const loginInfo = initialState.currentUser;
  const loginInfo = getAuthority();
  if (
    authority &&
    loginInfo &&
    loginInfo.permissions &&
    loginInfo.permissions.indexOf(authority) > -1
  ) {
    return true;
  }
  return false;
}

export default Authorized;
